package com.frontend.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.dto.ArticleDTO;
import com.common.exception.ModelNotFoundException;
import com.common.query.ArticleQueryParam;
import com.common.service.ArticleService;
import com.util.CommonPage;
import com.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "ArticleController", value = "文章管理")
public class ArticleController {

    @Resource
    ArticleService articleService;

    @ApiOperation("热门文章列表")
    @RequestMapping(value = "/article/hot", method = RequestMethod.GET)
    public CommonResult<List<ArticleDTO>> getHotArticle(
            @RequestParam(value = "size", defaultValue = "5") Integer size
    ) {
        if (size == 0) {
            return CommonResult.failed("size值无效");
        }
        List<ArticleDTO> articles = articleService.findHotArticlesWithUser(size);
        return CommonResult.success(articles);
    }

    @ApiOperation("最新文章列表")
    @RequestMapping(value = "/article/new", method = RequestMethod.GET)
    public CommonResult getNewArticle(
            @RequestParam(name = "size", defaultValue = "5") Integer size
    ) {

        if (size == 0) {
            return CommonResult.failed("size值无效");
        }

        List<ArticleDTO> articles = articleService.findNewArticlesWithUser(size);
        return CommonResult.success(articles);
    }

    @ApiOperation("文章信息")
    @RequestMapping(value = "/article/{id}", method = RequestMethod.GET)
    public CommonResult getArticleDetail(
            @PathVariable(value = "id") Long articleId
    ) {
        ArticleDTO article = articleService.findWithUserByArticleId(articleId);
        if (article == null) {
            return CommonResult.validateFailed("无效参数");
        }

        return CommonResult.success(article);
    }

    @ApiOperation("获取分类文章信息")
    @RequestMapping(value = "/article/category/{categoryId}")
    public CommonResult<List<ArticleDTO>> getArticleByCategory(
            @PathVariable("categoryId") Long categoryId
    ) {
        ArticleQueryParam articleQueryParam = new ArticleQueryParam();
        articleQueryParam.setCategoryId(categoryId);

        return CommonResult.success(articleService.findAll(articleQueryParam));
    }

    @ApiOperation("文章归档信息")
    @RequestMapping(value = "/article/archives", method = RequestMethod.GET)
    public CommonResult<List<Map>> getArticleArchives() {
        return CommonResult.success(articleService.getArticleArchives());
    }

    @ApiOperation("搜索某日的文章归档信息")
    @RequestMapping(value = "/article/archives/{dateRange}", method = RequestMethod.GET)
    public CommonResult getArticleByArchivesRange(
            @PathVariable("dataRange") String dataRange
    ) {
        return null;
    }

    @ApiOperation("文章列表")
    @RequestMapping(value = "/article", method = RequestMethod.GET)
    public CommonResult getArticle(
            ArticleQueryParam articleQueryParam,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        IPage<ArticleDTO> articles = articleService.findAll(page, pageSize, articleQueryParam);
        return CommonResult.success(CommonPage.restPage(articles));
    }

    @ApiOperation("文章阅读数++")
    @RequestMapping(value = "/article/inc-read-num/{id}", method = RequestMethod.PATCH)
    public CommonResult patchArticleReadNum(
            @PathVariable("id") Long id
    ) {

        try {
            boolean flag = articleService.readNumInc(id);

            if (flag) {
                return CommonResult.success("操作成功");
            } else {
                return CommonResult.failed();
            }
        } catch (ModelNotFoundException e) {
            return CommonResult.failed("id参数错误");
        }
    }


}
